<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *商品管理
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';

$return=array();
$tabs=array(
	array('op'=>'list','title'=>'商品列表'),
	array('op'=>'cj','title'=>'采集商品'),
	array('op'=>'listcj','title'=>'批量采集'),
);
if($_RQ['op']=='cj'){
	$set=$this->plugin_setting('basic');
	if(!$set['unionid']){
		exi('请先设置联盟id','','/setting/');
	}
	if($_RQ['postdata']){
		$post=get_postdata();
		$goods=(array)$this->model()->goodsInfo($post['sku']);
		foreach($goods as $v){
				//$moreinfo=$this->model()->goodsInfobigfield($v['skuId']);
				$images=$moreinfo['imageInfo']['imageList']['urlInfo'];
				$content=$moreinfo['baseBigFieldInfo']['wdis'];
				$skuid=pdo_getcolumn('jy_jdk_goods',array('skuId'=>$v['skuId'],'uniacid'=>$_SESSION['uniacid']),'skuId');
				if($skuid){
					$goodsid=pdo_getcolumn('jy_jdk_goods',array('skuId'=>$v['skuId'],'uniacid'=>$_SESSION['uniacid']),'goodsid');
					$good=array(
								'title'=>$v['goodsName'],
								'image'=>$v['imgUrl'],
								'images'=>$images,
								'content'=>$content,
								'originalprice'=>$v['unitPrice']*1.5,
								'price'=>$v['unitPrice'],
					);
					pdo_update('jy_weishop_goods',$good,array('id'=>$goodsid,'uniacid'=>$_SESSION['uniacid']));
					unset($v['isFreeFreightRisk']);
					unset($v['inOrderCount']);
					pdo_update('jy_jdk_goods', $v,array('skuId'=>$v['skuId'],'uniacid'=>$_SESSION['uniacid']));
				}else{
					$good=array(
								'uniacid'=>$_SESSION['uniacid'],
								'title'=>$v['goodsName'],
								'images'=>$images,
								'content'=>$content,
								'image'=>$v['imgUrl'],
								'originalprice'=>$v['unitPrice']*1.5,
								'price'=>$v['unitPrice'],
								'pluginname'=>'jy_jdk',
								'createtime'=>time()
								
					);
					pdo_insert('jy_weishop_goods',$good);
					$goodsid=pdo_insertid();
					$v['goodsid']=$goodsid;
					$v['uniacid']=$_SESSION['uniacid'];
					$v['createtime']=time();
					unset($v['isFreeFreightRisk']);
					unset($v['inOrderCount']);
					$url=$this->model()->geturl($v['materialUrl']);
					$v['url']=$url['shortURL']?:$url['clickURL'];
					pdo_insert('jy_jdk_goods', $v);
				}
				exi('采集成功','','list');
		}
	}
	$htmls=array(
		'type'=>'edit',
		'desc'=>'采集商品',
		'tabs'=>array(
			array('op'=>'list','title'=>'列表'),			
		),	
		'edit'=>array(
			array('title'=>'1、先去京东客联盟申请https://union.jd.com/</br>2、申请成功后获得联盟ID和授权Key，填到本插件的基本设置-基本设立</br>3、进入京东联盟后台https://union.jd.com/，商品推广--》打开商品--》浏览器网址，找到一串数字：例如：https://item.jd.com/10059094026107.html，其中10059094026107就是商品skuID，粘贴到下面！点击采集！','type'=>'tip'),
			array('name'=>'sku','title'=>'skuID','type'=>'text','help'=>'京东商品编号，逗号分割，最多100个'),
					
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='listcj'){
	$set=$this->plugin_setting('basic');	
	if(!$set['unionid']){
		exi('请先设置联盟id','','/setting/');
	}
	$cat_ids=$this->plugin_setting('cat_ids');
	if(!$cat_ids){
		$cat_ids=$this->model()->getgoods_cats();
		if($cat_ids && $cat_ids['categoryResp']){
			$this->plugin_setting('cat_ids',$cat_ids['categoryResp']);
		}else{
			$nocatstip='<red>参数设置异常，无法获取到分类信息！</red>';
		}
	}
	if($_RQ['postdata']){
		$post=get_postdata();
		if(intval($post['page']) !=$post['page']){
			exi('采集第几页数，必须是整数！','error');
		}
		if($post['type']==0){//常规条件采集
			$re=$this->model()->goodslist($post);
			$re['goods_list']=$re['goodsResp'];
		}elseif($post['type']==1){//按精选
			$re=$this->model()->getgoodslist_jin($post['store_id'],$post);
			$re['goods_list']=$re['jfGoodsResp'];
		}elseif($post['type']==2){//秒杀
			$re=$this->model()->getgoodslist_miao($post);
			$re['goods_list']=$re['secKillGoodsResp'];
		}elseif($post['type']==3){//学生
			$re=$this->model()->getgoodslist_xue($post);
			$re['goods_list']=$re['stuPriceGoodsResp'];
		}
		if($re){
				if(count($re['goods_list'])==0){
					exi('没用采集到任何商品，换个条件试试！','error');
				}
				if($re['goods_list']){
					foreach($re['goods_list'] as $v){
						$v=(array)$v;
						$this->model()->creategoods($v);
					}
				}else{
					$this->model()->creategoods($re);
				}
				exi('采集成功','','list');
		}else{
			exi('采集失败','error');
		}
		
	}
	$htmls=array(
		'type'=>'edit',
		'desc'=>'采集商品',
		'tabs'=>$tabs,
		'edit'=>array(
			array('title'=>'1、先去京东客联盟申请https://union.jd.com/</br>2、申请成功后获得联盟ID和授权Key，填到本插件的基本设置-基本设立</br>3、[申请批量采集权限]：批量采集必须申请接口权限才行！！','type'=>'tip'),
			array('name'=>'type','title'=>'采集方法','type'=>'radio','options'=>'types'),
			array('name'=>'cid1','title'=>'商品类目','type'=>'select','options'=>'cat_ids','option_value'=>'cat_id','option_title'=>'cat_name','show'=>array('type'=>array(0,2,3)),'help'=>$nocatstip),
			array('name'=>'isHot','title'=>'是否是爆款','type'=>'switch','show'=>array('type'=>0),'help'=>''),
			array('name'=>'isCoupon','title'=>'是否是优惠券商品','type'=>'switch','show'=>array('type'=>0),'help'=>''),
			array('name'=>'isPG','title'=>'是否是拼购商品','type'=>'switch','show'=>array('type'=>0),'help'=>''),
			array('title'=>'拼购价格','name1'=>'pingouPriceStart','title1'=>'拼购价格区间开始','unit1'=>'','name2'=>'pingouPriceEnd','title2'=>'拼购价格区间结束','type'=>'string_two','show'=>array('type'=>0),'help'=>''),
			array('name'=>'keyword','title'=>'商品关键词','type'=>'string','show'=>array('type'=>0),'help'=>''),
			array('name'=>'owner','title'=>'店铺类型','type'=>'radio','options'=>'owners','show'=>array('type'=>array(0,2,3)),'help'=>''),
			array('name'=>'sortName','title'=>'排序条件','type'=>'select','options'=>'sortNames','show'=>array('type'=>0),'help'=>''),
			array('name'=>'sorts','title'=>'升降序','type'=>'radio','options'=>'sorts','help'=>''),
			array('title'=>'价格范围','name1'=>'pricefrom','title1'=>'区间的开始值','unit1'=>'','name2'=>'priceto','title2'=>'区间的结束值','type'=>'string_two','show'=>array('type'=>0),'help'=>''),
			array('title'=>'佣金范围','name1'=>'commissionShareStart','title1'=>'区间的开始值','unit1'=>'','name2'=>'commissionShareEnd','title2'=>'区间的结束值','type'=>'string_two','show'=>array('type'=>array(0,2,3)),'help'=>''),
			array('name'=>'brandCode','title'=>'品牌code','type'=>'string','show'=>array('type'=>0),'help'=>''),
			array('name'=>'shopId','title'=>'店铺ID','type'=>'string','show'=>array('type'=>0),'help'=>''),
			
			array('name'=>'sortName2','title'=>'排序条件','type'=>'select','options'=>'sortNames2','show'=>array('type'=>1),'help'=>''),
			array('name'=>'eliteId','title'=>'商品标签','type'=>'radio','options'=>'eliteIds','show'=>array('type'=>1),'help'=>''),
			
			array('name'=>'sortName3','title'=>'排序条件','type'=>'select','options'=>'sortNames3','show'=>array('type'=>array(2,3)),'help'=>''),
			array('name'=>'isBeginSecKill','title'=>'是否返采集开始秒杀商品','type'=>'switch','show'=>array('type'=>2),'help'=>''),
			array('title'=>'秒杀价区间','name1'=>'secKillPriceFrom','title1'=>'区间开始','unit1'=>'','name2'=>'secKillPriceTo','title2'=>'区间结束','type'=>'string_two','show'=>array('type'=>2),'help'=>''),
			
			array('title'=>'学生专享价区间','name1'=>'stuPriceFrom','title1'=>'区间开始','unit1'=>'','name2'=>'stuPriceTo','title2'=>'区间结束','type'=>'string_two','show'=>array('type'=>3),'help'=>''),
			array('name'=>'pageIndex','title'=>'采集第几页','type'=>'string'),
		)
	);
	$return['owners']=array('g'=>'自营','p'=>'POP');
	$return['sorts']=array('asc'=>'升序','desc'=>'降序');
	$return['sortNames']=array('price'=>'单价','commissionShare'=>'佣金比例','commission'=>'佣金','inOrderCount30Days'=>'30天引单量','inOrderComm30Days'=>'30天支出佣金');
	$return['sortNames2']=array('price'=>'单价','commissionShare'=>'佣金比例','commission'=>'佣金','inOrderCount30DaysSku'=>'sku维度30天引单量','comments'=>'评论数','goodComments'=>'好评数');
	$return['sortNames3']=array('seckillPrice'=>'单价','commissionShare'=>'佣金比例','inOrderCount30Days'=>'30天引单量','inOrderComm30Days'=>'30天支出佣金');
	$return['types']=array(0=>'常规条件采集',1=>'京粉精选商品',2=>'秒杀商品',3=>'学生价商品');
	$return['eliteIds']=array(
		'1'=>'好券商品',
		'2'=>'精选卖场',
		'10'=>'9.9包邮',
		'15'=>'京东配送',
		'22'=>'实时热销榜',
		'23'=>'为你推荐',
		'24'=>'数码家电',
		'25'=>'超市',
		'26'=>'母婴玩具',
		'27'=>'家具日用',
		'28'=>'美妆穿搭',
		'29'=>'医药保健',
		'30'=>'图书文具',
		'31'=>'今日必推',
		'32'=>'京东好物',
		'33'=>'京东秒杀',
		'34'=>'拼购商品',
		'40'=>'高收益榜',
		'41'=>'自营热卖榜',
		'109'=>'新品首发',
		'110'=>'自营',
		'125'=>'首购商品',
		'129'=>'高佣榜单',
		'130'=>'视频商品',
		'153'=>'历史最低价商品榜',
	);
	$return['cat_ids']=$this->plugin_setting('cat_ids');
	$return['opt_ids']=$this->plugin_setting('opt_ids');
	$return['htmls']=$htmls;
	exi($return);
}
//上架商品
if($_RQ['op']=='list'){
	$set=$this->plugin_setting('basic');
	$params=array();
	$params['uniacid']=$_SESSION['uniacid'];	
	if(!empty($_RQ['goodsName'])){
		$params['goodsName like']='%'.trim($_RQ['goodsName']).'%';
		$return['params']['goodsName']=$_RQ['goodsName'];
	}
	
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	if($_RQ['num']){
		$psize=$_RQ['num'];
	}
	$total='';
	$fields=array('id','createtime','goodsid','uniacid','goodsName','cid3Name','cidName','materialUrl','url','paixu','isnew','ishot','cid2Name','startDate','endDate','imgUrl','skuId','unitPrice','commisionRatioPc','commisionRatioWl','wlUnitPrice');
	$list=pdo_getslice('jy_jdk_goods',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
				$item['startDate']=date('Y/m/d H:i:s',$item['startDate']);
				$item['endDate']=date('Y/m/d H:i:s',$item['endDate']);
				$item['jyurl']=custom_url('jy_jdk/detail//id='.$item['id'],true);
				$goods=pdo_get('jy_weishop_goods',array('id'=>$item['goodsid'],'uniacid'=>$_SESSION['uniacid']),array('saletype','paixu'));
				$item['saletype']=$goods['saletype'];
				$item['paixu']=$goods['paixu'];
				if(!$item['url']){
					$re=$this->model()->geturl($item['materialUrl'],$set['unionid']);
					$item['url']=$re['shortURL']?:$url['clickURL'];
					pdo_update('jy_jdk_goods',array('url'=>$item['url'],'createtime'=>time()),array('id'=>$item['id'],'uniacid'=>$_SESSION['uniacid']));
				}
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','tip'=>'【<red>温馨提示</red>】：1、要获取商品详情，商品幻灯片，优惠券领取等信息，需移步京东联盟单独申请API接口权限(高级API申请方式：https://union.jd.com/helpcenter/13246-13313-46270)；</br>2、未开启《工具商获取推广链接接口【申请】》的无法获得推广的短URL，只能获取长URL。','del_one'=>true,'delall'=>true,'list'=>array(
			array('title'=>'商品名称','name'=>'goodsName','type'=>'string'),
			array('type'=>'img_string','title'=>'商品信息','img'=>'imgUrl',
			'data'=>array(
				array('title'=>'价格：￥','name'=>'unitPrice')
				)
			),
			array('type'=>'img_string','title'=>'商品分类',
			'data'=>array(
				array('title'=>'一级：','name'=>'cidName'),
				array('title'=>'二级：','name'=>'cid2Name'),
				array('title'=>'三级：','name'=>'cid3Name')
				)
			),
			array('name'=>'paixu','title'=>'排序','type'=>'string'),
			array('name'=>'saletype','title'=>'属性','type'=>'radio','options'=>'saletype'),
			array('title'=>'佣金比例','name'=>'commisionRatioPc','type'=>'string'),
			array('title'=>'skuid','name'=>'skuId','type'=>'string'),
			array('name'=>'materialUrl','title'=>'京东链接','type'=>'copy'),
			array('name'=>'url','title'=>'推广链接','type'=>'copy'),
			array('name'=>'jyurl','title'=>'本地链接','type'=>'copy'),
		)),
		'search_one'=>array('name'=>'goodsName','title'=>'商品名称','add'=>false),
	);
	$return['status']=array('0'=>'否','1'=>'是');
	$return['saletype']=array(
		'1'=>'推荐_Recommended',
		'2'=>'新品_New',
		'3'=>'热卖_Hot',
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='edit'){
	$id=intval($_RQ['id']);
	if($id){
		$goodsid=pdo_getcolumn('jy_jdk_goods',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']),'goodsid');
		if($goodsid){
			$item=pdo_get('jy_weishop_goods',array('id'=>$goodsid,'uniacid'=>$_SESSION['uniacid']),array('saletype','paixu'));
			$item['goodsid']=$goodsid;
		}else{
			$jdk=pdo_get('jy_jdk_goods',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']),array('goodsName','imgUrl','unitPrice'));
			$good=array(
							'uniacid'=>$_SESSION['uniacid'],
							'title'=>$v['goodsName'],
							'image'=>$v['imgUrl'],
							'originalprice'=>$v['unitPrice']*1.5,
							'price'=>$v['unitPrice'],
							'pluginname'=>'jy_jdk',
							'createtime'=>time()
							
			);
			pdo_insert('jy_weishop_goods',$good);
			$goodsid=pdo_insertid();
			pdo_update('jy_jdk_goods',array('goodsid'=>$goodsid),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
			$item=array('goodsid'=>$goodsid);
		}
		
	}else{
		exi('参数错误','','list');
	}
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'desc'=>'商品信息',
		'tabs'=>array(
			array('op'=>'list','title'=>'商品列表'),
			
		),
		'edit'=>array(
			array('name'=>'paixu','title'=>'排序','type'=>'string'),
			array('name'=>'saletype','title'=>'商品属性','type'=>'checkbox','options'=>'saletype'),
		)
	);
	$return['htmls']=$htmls;
	$return['saletype']=array(
		'1'=>'推荐_Recommended',
		'2'=>'新品_New',
		'3'=>'热卖_Hot',
	);
	$return['status']=array('0'=>'否','1'=>'是');
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();	
	$id=intval($postdata['goodsid']);
	if($id){
		pdo_update('jy_weishop_goods',array('paixu'=>intval($postdata['paixu']),'saletype'=>$postdata['saletype']),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		exi('修改成功！','','list');
	}else{
		exi('参数错误','error');
	}
}

if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			pdo_delete('jy_jdk_goods',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
			$goodsid=pdo_getcolumn('jy_jdk_goods',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']),'goodsid');
			pdo_delete('jy_weishop_goods',array('id'=>$goodsid,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}